package com.emmmya.ocs.common.converter;

import com.alibaba.fastjson2.JSON;
import org.thymeleaf.util.StringUtils;

import javax.persistence.AttributeConverter;

/**
 * @author laijieguan@sunwodao.com
 * @version 1.0
 * @date 2022-07-28 15:20:51
 */
public class JpaConverterObjectJson implements AttributeConverter<Object, String> {

    @Override
    public String convertToDatabaseColumn(Object o) {
        if(o == null){
            return null;
        }
        return JSON.toJSONString(o);
    }

    @Override
    public Object convertToEntityAttribute(String s) {
        if(StringUtils.isEmpty(s)){
            return null;
        }
        Object obj = JSON.parse(s);
        return obj;
    }
}
